<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="vi" lang = "vi">
<head>
<meta http-equiv="content-type"  content="text/html; charset=utf-8"/>
<title>Thanh toan bang the cao</title>
<style type="text/css">
em{
    color:red;
}
</style> 
</head>
<body>


<div id="wrapper">
<?php
if(isset($_POST['submit'])){
$required =  Array('Type','Pin','Serial');
$errors=array();
foreach($required as $fieldname ){
    if(!isset($_POST[$fieldname])|| empty($_POST[$fieldname])){
      $errors[] = "Mục <em>".$fieldname."</em> bị bỏ trống." ;
       
    }        
}

$numeric =  Array('Pin','Serial');
foreach($numeric as $field ){
    if(!isset($_POST[$field])|| !is_numeric($_POST[$field])){
       $errors[]="Mục <em>".$field."</em> phải điền đúng thông tin thẻ cào."  ;
       
    }        
}
// process the form
$Type=$_POST['Type'];
$Pin=$_POST['Pin'];
$Serial=$_POST['Serial'];
$accesskey=$_POST['accesskey'];
// function

function execPostRequest($url,$data){
    //open connection
    $ch = curl_init();
   
    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $data);           
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    //execute post
    $result = curl_exec($ch);         
   
    //close connection
    curl_close($ch);
    return $result;
}



//Get getCode
if(empty($errors)){
$str_data='accesskey='.$accesskey.'&serial='.$Serial.'&pin='.$Pin.'&type='.$Type;
$getCode=execPostRequest('http://sms.vn/card-charging-api', $str_data);

}



} //End isset if


?>


<form action="" method="post">

<input type="hidden" id="accessKey" name="accesskey"
		value="c7d094a57379cb883ddc500dd0c370fc" />


<label
				style="display: block; padding-bottom: 5px; font: 'Arial', Helvetica, sans-serif">
				<strong>Mã số thẻ:</strong>
			</label>
<input type="text" name="Pin" value ="<?php if(isset($Pin)){echo $Pin;}?>" style="margin: 0px; border: 1px solid #ccc; width: 250px; border-radius: 5px; height: 25px; line-height: 25px; padding: 0px 10px; font-size: 14px; -webkit-box-shadow: 0 0 0 #000, inset 0px 3px 3px #eee; box-shadow: 0 0 0 #000, inset 0px 3px 3px #eee;"
				 size="45"/>
</p>


<label 
				style="display: block; padding-bottom: 5px; font: 'Arial', Helvetica, sans-serif">
				<strong>Số serri:</strong>
			</label>
            
<input type="text" name="Serial" value="<?php if(isset($Serial)){echo $Serial;}?>" style="margin: 0px; border: 1px solid #ccc; width: 250px; border-radius: 5px; height: 25px; line-height: 25px; padding: 0px 10px; font-size: 14px; -webkit-box-shadow: 0 0 0 #000, inset 0px 3px 3px #eee; box-shadow: 0 0 0 #000, inset 0px 3px 3px #eee;"
				 size="45" />
</p>

<label	style="display: block; padding-bottom: 5px; font: 'Arial', Helvetica, sans-serif">
<p><strong>Loại Thẻ :</strong> </label>
<select name="Type" style="padding: 5px; border: 1px solid #ccc; width: 150px; border-radius: 5px; margin: 0px; -webkit-box-shadow: 0 0 0 #000, inset 0px 3px 3px #eee; box-shadow: 0 0 0 #000, inset 0px 3px 3px #eee;">


<option value="">--Chọn loại thẻ cào-- </option>
<?php
$Types=array('VTT'=>'Viettel','VNP'=>'Vinaphone','VMS'=>'Mobifone');
foreach($Types as $key => $value){
    echo"<option value='{$key}'";
    $temp = isset($Type) ? $Type : false;
    if($temp == $key){echo "selected='selected'";}
    echo">".$value."</option>";
}
?>
</select>
</p>

<p><input type="submit" name="submit" value="Nạp Tiền" /></p>


</form>
<ul>
<?php
$db_hostname ="localhost";
$db_username="2212dbuser";
$db_password='cs22124lif3!';
$db_name ="cs2212";
$dbh =mysl_connect($db_hostname,$db_username,$db_password) or die("Error:".mysql_error());
mysql_select_db($db_username);
if( isset($getCode)&&$getCode>=20000){
echo "<p>Bạn đã thanh toán thành công ".$getCode." VND. <a href='https://s3.amazonaws.com/Vieclam/00534djfhapqhuhfapefdfjalpw.rar'>Bấm đây để download</a></p>";}
else if(isset($getCode)){
    echo "Lưu ý nhập đúng mệnh giá thẻ cào 20,000 VND và thông tin chính xác.";
}
?>
<?php if(!empty($errors)){
    echo "<p><strong>Cập nhật ".count($errors)." thông tin như sau:</strong></p>";
    foreach($errors as $error){
        echo"<li>".$error."</li>";
    }
} ?>
</ul>
</div>
</body>
</html>